package com.xthh._function._language;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;


import com.xthh._function.HH_CharUtil;

import java.util.HashMap;
import java.util.Locale;


public class HH_LanguageUtil {

    public static final String ENGLISH = "en_US";
    public static final String CHINESE = "zh_CN";
    public static final String TAIWAN = "zh_TW";
    public static final String FOLLOWSYSTEM = "FOLLOWSYSTEM"; // 跟随系统切换

    private static HashMap<String, Locale> languagesList = new HashMap<String, Locale>(2) {{
        put(ENGLISH, Locale.ENGLISH);
        put(CHINESE, Locale.CHINESE);
        put(TAIWAN, Locale.TAIWAN);
    }};


    // 获取配置文件中的语言
    public static Locale GetLocalLanguage(Context ctx){
        return getLocaleByLanguage(ctx, HH_SpUserUtils.getString(ctx, "language"));
    }




    // 获取指定语言的locale信息，如果指定语言不存在
    // 返回本机语言，如果本机语言不是语言集合中的一种，返回英语
    public static Locale getLocaleByLanguage(Context ctx, String language) {
        if (isContainsKeyLanguage(language)) {
            return languagesList.get(language);
        } else {
            Locale locale = Locale.getDefault();
            for (String key : languagesList.keySet()) {
                if (TextUtils.equals(languagesList.get(key).getLanguage(), locale.getLanguage())) {
                    return locale;
                }
            }
        }

        // 设置默认语言为英文
        HH_SpUserUtils.putString(ctx, "language", HH_LanguageUtil.ENGLISH);
        return Locale.ENGLISH;
    }

    // 如果此映射包含指定键的映射关系，则返回 true
    private static boolean isContainsKeyLanguage(String language) {
        return languagesList.containsKey(language);
    }

    // 设置语言
    public static void SetCurrentLanguange(Context ctx) {
        // 根据配置文件切换
        Resources resources = ctx.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = HH_LanguageUtil.GetLocalLanguage(ctx);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }

    //获取当前语言
    public static Context getAttachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context);
        } else {
            //7.0之前的更新语言资源方式
            changeResLanguage(context);
            return context;
        }
    }
    private static void changeResLanguage(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(getLocale(context));
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }



    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(getLocale(context));
        return context.createConfigurationContext(configuration);
    }



    private static Locale getLocale(Context ctx){
        return getCurrentLanguage(ctx);
    }

    public static Locale getCurrentLanguage(Context context){
        String sLanguange = HH_SpUserUtils.getString(context, "language");
        if (HH_CharUtil.Strcmp(sLanguange, HH_LanguageUtil.ENGLISH, true))
            return Locale.ENGLISH;
        else if (HH_CharUtil.Strcmp(sLanguange, HH_LanguageUtil.CHINESE, true))
            return Locale.CHINA;
        else if (HH_CharUtil.Strcmp(sLanguange, HH_LanguageUtil.TAIWAN, true))
            return Locale.TAIWAN;
        else {
            return Locale.ENGLISH;
        }
    }

    public static void SetCurrentActivityLanguage(Context ctx){
        // 判断是不是需要跟随系统的设置
        String sLanguage = HH_SpUserUtils.getString(ctx, "language");
        if (sLanguage == null || sLanguage.length() <= 0 || HH_CharUtil.Strcmp(sLanguage, HH_LanguageUtil.FOLLOWSYSTEM, true)) {
            // 随系统语言切换
            HH_SpUserUtils.putString(ctx, "language", HH_LanguageUtil.FOLLOWSYSTEM);
        } else {
            // 根据配置文件切换
            Resources resources = ctx.getResources();
            Configuration configuration = resources.getConfiguration();
            Locale locale = HH_LanguageUtil.getLocaleByLanguage(ctx, HH_SpUserUtils.getString(ctx, "language"));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLocale(locale);
            } else {
                configuration.locale = locale;
            }
            DisplayMetrics dm = resources.getDisplayMetrics();
            resources.updateConfiguration(configuration, dm);
        }
    }

    public static boolean IsChineseLanguage(Context ctx){
        String sLanguange = HH_SpUserUtils.getString(ctx, "language");
        // 获取当前语言
        if (HH_CharUtil.Strcmp(sLanguange, HH_LanguageUtil.FOLLOWSYSTEM, true)){
            if (HH_CharUtil.Strcmp(Locale.getDefault().toString(), "zh_CN", true))
                return true;
            else
                return false;
        } else if (HH_CharUtil.Strcmp(sLanguange, HH_LanguageUtil.CHINESE, true)){
            return true;
        }

        return false;
    }

    /**
     * 修改语言
     *
     * @param activity 上下文
     * @param language 例如修改为 英文传“en”，参考上文字符串常量
     * @param cls      要跳转的类（一般为入口类）
     */
    public static void changeAppLanguage(Context ctx, Activity activity, String language, Class<?> cls) {
        Resources resources = activity.getResources();
        Configuration configuration = resources.getConfiguration();
        // app locale 默认简体中文
        Locale locale = getLocaleByLanguage(ctx, (language == null || language.length() <= 0)? "zh" : language);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);

        Log.e("Log","设置的语言：" + language);

    }
}
